<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Category;
use app\models\Article;

class SiteController extends Controller
{
    public function actionInsert()
    {
        $c=new Category();
        $c->name='军事';
        $c->save();
        $c=new Category();
        $c->name='政治';
        $c->save();
        $c=new Category();
        $c->name='娱乐';
        $c->save();
    }
    public function actionInsert2()
    { 
    	$c=new Article();
    	$c->title='政治111';
    	$c->cate_id=5;
    	$c->save();
    	
        $c=new Article();
    	$c->title='政治222';
    	$c->cate_id=5;
    	$c->save();
    	$c=new Article();
    	$c->title='政治333';
    	$c->cate_id=5;
    	$c->save();
    	
    	$c=new Article();
    	$c->title='军事111';
    	$c->cate_id=4;
    	$c->save();
    	 
    	$c=new Article();
    	$c->title='军事222';
    	$c->cate_id=4;
    	$c->save();
    	$c=new Article();
    	$c->title='军事333';
    	$c->cate_id=4;
    	$c->save();
    }
    public function actionFind(){
    	$c=Category::findone(5);
//     	p($c);
// 方式1
//     	$arr=Article::find()->where(['cate_id'=>$c->id])->all();
// 方式2
// $arr=$c->hasMany('app\models\Article',['cate_id'=>'id'])->all();
// 方式3
$arr=$c->hasMany(Article::className(),['cate_id'=>'id'])->asArray()->all();
    	p($arr);
    	
    }
}
